home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / SequenceInputStream.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  183 lines

  1. /*
  2.  * @(#)SequenceInputStream.java    1.15 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.io.InputStream;
  18. import java.util.Enumeration;
  19. import java.util.Vector;
  20.  
  21. /**
  22.  * The sequence input stream class allows an application to combine 
  23.  * several input streams serially and make them appear as if they 
  24.  * were a single input stream. Each input stream is read from, in 
  25.  * turn, until it reaches the end of the stream. The sequence input 
  26.  * stream class then closes that stream and automatically switches to 
  27.  * the next input stream. 
  28.  *
  29.  * @author  Author van Hoff
  30.  * @version 1.15, 07/01/98
  31.  * @since   JDK1.0
  32.  */
  33. public
  34. class SequenceInputStream extends InputStream {
  35.     Enumeration e;
  36.     InputStream in;
  37.     
  38.     /**
  39.      * Constructs a new sequence input stream initialized to the 
  40.      * specified enumeration of input streams. Each object in the 
  41.      * enumeration must be an <code>InputStream</code>. 
  42.      *
  43.      * @param   e   an enumeration of input streams.
  44.      * @see     java.util.Enumeration
  45.      * @since   JDK1.0
  46.      */
  47.     public SequenceInputStream(Enumeration e) {
  48.     this.e = e;
  49.     try {
  50.         nextStream();
  51.     } catch (IOException ex) {
  52.         // This should never happen
  53.         throw new Error("panic");
  54.     }
  55.     }
  56.   
  57.     /**
  58.      * Constructs a new sequence input stream initialized to read first 
  59.      * from the input stream <code>s1</code>, and then from the input 
  60.      * stream <code>s2</code>. 
  61.      *
  62.      * @param   s1   the first input stream to read.
  63.      * @param   s2   the second input stream to read.
  64.      * @since   JDK1.0
  65.      */
  66.     public SequenceInputStream(InputStream s1, InputStream s2) {
  67.     Vector    v = new Vector(2);
  68.  
  69.     v.addElement(s1);
  70.     v.addElement(s2);
  71.     e = v.elements();
  72.     try {
  73.         nextStream();
  74.     } catch (IOException ex) {
  75.         // This should never happen
  76.         throw new Error("panic");
  77.     }
  78.     }
  79.    
  80.     /**
  81.      *  Continues reading in the next stream if an EOF is reached.
  82.      */
  83.     final void nextStream() throws IOException {
  84.     if (in != null) {
  85.         in.close();
  86.     }
  87.     in = e.hasMoreElements() ? (InputStream) e.nextElement() : null;
  88.     }
  89.  
  90.     /**
  91.      * Returns the number of bytes available on the current stream.
  92.      *
  93.      * @since   JDK1.1
  94.      */
  95.     public int available() throws IOException {
  96.     if(in == null) {
  97.         return 0; // no way to signal EOF from available()
  98.     } 
  99.     return in.available();
  100.     }
  101.  
  102.     /**
  103.      * Reads the next byte of data from this input stream. The byte is 
  104.      * returned as an <code>int</code> in the range <code>0</code> to 
  105.      * <code>255</code>. If no byte is available because the end of the 
  106.      * stream has been reached, the value <code>-1</code> is returned. 
  107.      * This method blocks until input data is available, the end of the 
  108.      * stream is detected, or an exception is thrown. 
  109.      * <p>
  110.      * The <code>read</code> method of <code>SequenceInputStream</code> 
  111.      * tries to read one character from the current substream. If it 
  112.      * reaches the end of the stream, it calls the <code>close</code> 
  113.      * method of the current substream and begins reading from the next 
  114.      * substream. 
  115.      *
  116.      * @return     the next byte of data, or <code>-1</code> if the end of the
  117.      *             stream is reached.
  118.      * @exception  IOException  if an I/O error occurs.
  119.      * @since      JDK1.0
  120.      */
  121.     public int read() throws IOException {
  122.     if (in == null) {
  123.         return -1;
  124.     }
  125.     int c = in.read();
  126.     if (c == -1) {
  127.         nextStream();
  128.         return read();
  129.     }
  130.     return c;
  131.     }
  132.  
  133.     /**
  134.      * Reads up to <code>len</code> bytes of data from this input stream 
  135.      * into an array of bytes. This method blocks until at least 1 byte 
  136.      * of input is available. If the first argument is <code>null</code>, 
  137.      * up to <code>len</code> bytes are read and discarded. 
  138.      * <p>
  139.      * The <code>read</code> method of <code>SequenceInputStream</code> 
  140.      * tries to read the data from the current substream. If it fails to 
  141.      * read any characters because the substream has reached the end of 
  142.      * the stream, it calls the <code>close</code> method of the current 
  143.      * substream and begins reading from the next substream. 
  144.      *
  145.      * @param      b     the buffer into which the data is read.
  146.      * @param      off   the start offset of the data.
  147.      * @param      len   the maximum number of bytes read.
  148.      * @exception  IOException  if an I/O error occurs.
  149.      * @since      JDK1.0
  150.      */
  151.     public int read(byte buf[], int pos, int len) throws IOException {
  152.     if (in == null) {
  153.         return -1;
  154.     } else if (len == 0) { 
  155.         return 0;
  156.     }
  157.     int n = in.read(buf, pos, len);
  158.     if (n <= 0) {
  159.         nextStream();
  160.         return read(buf, pos, len);
  161.     }
  162.     return n;
  163.     }
  164.  
  165.     /**
  166.      * Closes this input stream and releases any system resources 
  167.      * associated with the stream. 
  168.      * <p>
  169.      * The <code>close</code> method of <code>SequenceInputStream</code> 
  170.      * calls the <code>close</code> method of both the substream from 
  171.      * which it is currently reading and the <code>close</code> method of 
  172.      * all the substreams that it has not yet begun to read from. 
  173.      *
  174.      * @exception  IOException  if an I/O error occurs.
  175.      * @since      JDK1.0
  176.      */
  177.     public void close() throws IOException {
  178.     do {
  179.         nextStream();
  180.     } while (in != null);
  181.     }
  182. }
  183.